﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DTO;
using DAO;
using System.Data.SqlClient;
using System.IO;

namespace GUI
{
    public partial class FormQuanLy : Form
    {
        public FormQuanLy()
        {
            InitializeComponent();
        }
        public string[] txt = new string[4];
        private void readTXT(string filepath)
        {
            //FileStream fs = new FileStream(filepath, FileMode.Open);
            //StreamReader rd = new StreamReader(fs, Encoding.Unicode);
            string[] lines = System.IO.File.ReadAllLines(filepath);

            int i = 0;
            foreach (string line in lines)
            {
                txt[i] = line;
                i++;
            }
            if (txt[0] == null)
            {
                frmSetting frmSt = new frmSetting();
                frmSt.frmCha2 = this;
                frmSt.ShowDialog();
                if (txt[0] != null)
                {
                    File.SetAttributes(filepath, FileAttributes.Normal);
                    StreamWriter sw = new StreamWriter(filepath);
                    sw.WriteLine(txt[0]);
                    sw.WriteLine(txt[1]);
                    sw.WriteLine(txt[2]);
                    sw.WriteLine(txt[3]);
                    sw.Close();
                }
                File.SetAttributes(filepath, FileAttributes.ReadOnly);
            }
        }
        //public DataProvider da;
        string dB = "";
        private void initConn()
        {
           // da = new DataProvider();
            DataProvider.NamePC = txt[0];
            DataProvider.DataBase = "master";
            dB = txt[1];
            DataProvider.UID = txt[2];
            DataProvider.Pwd = txt[3];
        }
        public void AnHienChucNang(int i)
        {
            switch (i)
            {
                //Bàn, nhân viên, thực đơn, gọi món, hóa đơn, đổi mật khẩu, DL bàn, DL thực đơn, DL nhân viên, Sao lưu & phục hồi
                case 1:
                    bànToolStripMenuItem.Enabled = true;
                    break;
                case 2:
                    nhânViênToolStripMenuItem.Enabled = true;
                    break;
                case 3:
                    thựcĐơnToolStripMenuItem.Enabled = true;
                    break;
                case 4:
                    gọiMónToolStripMenuItem.Enabled = true;
                    break;
                case 5:
                    hóaĐơnToolStripMenuItem.Enabled = true;
                    break;
                case 6:
                    đổiMậtKhẩuToolStripMenuItem1.Enabled = true;
                    break;
                case 7:
                    bànToolStripMenuItem1.Enabled = true;
                    break;
                case 8:
                    thựcĐơnToolStripMenuItem1.Enabled = true;
                    break;
                case 9:
                    saoLưuToolStripMenuItem.Enabled = true;
                    phụcHồiToolStripMenuItem.Enabled = true;
                    break;
            }
        }
        public NHANVIENDTO NVDTO;

        private void FormQuanLy_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }
        private void saoLưuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog o = new SaveFileDialog();
            o.Filter = "Backup file (*.bak)|*.bak";
            if (o.ShowDialog() == DialogResult.OK)
            {
                SqlConnection conn = DataProvider.ConnectDB();
                string dir = "use master BACKUP DATABASE [" + DataProvider.DataBase + "] TO DISK='" + o.FileName + "'";
                try
                {
                    SqlCommand command = new SqlCommand(dir, conn);
                    command.ExecuteNonQuery();
                    conn.Close();
                    MessageBox.Show("Đã sao lưu cơ sở dữ liệu");
                    return;
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message);
                }
                conn.Close();
            }
        }

        private void phụcHồiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string MyDocumentOfPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            if (!Directory.Exists("QLNhaHang"))
            {
                Directory.CreateDirectory(MyDocumentOfPath + "\\QLNhaHang");
            }
            MyDocumentOfPath += "\\QLNhaHang\\setting.txt";
            if (!File.Exists(MyDocumentOfPath))
            {
                File.CreateText(MyDocumentOfPath).Close();
                File.SetAttributes(MyDocumentOfPath, FileAttributes.ReadOnly);
            }
            readTXT(MyDocumentOfPath);
            initConn();
            
            OpenFileDialog o = new OpenFileDialog();
            o.Filter = "Backup file (*.bak)|*.bak";
            if (o.ShowDialog() == DialogResult.OK)
            {
                SqlConnection conn = DataProvider.ConnectDB2();
                string dir = "Restore Database  [" + dB + "]  from disk='" + o.FileName + "'";
                try
                {
                    SqlCommand command = new SqlCommand(dir, conn);
                    command.ExecuteNonQuery();
                    conn.Close();
                }
                catch (SqlException ex)
                {
                    conn.Close();
                    //MessageBox.Show(ex.Message);
                }
                finally
                {
                    conn.Close();
                    FormDangNhap frm = new FormDangNhap();
                    frm.FormDangNhap_Load(sender, e);
                }
            }
        }
        private void bànToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormQuanLyTangBan frm = new FormQuanLyTangBan();
            frm.ShowDialog();
        }
        private void thựcĐơnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormQuanLyThucDon frm = new FormQuanLyThucDon();
            frm.ShowDialog();
        }
        private void nhânViênToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormNhanVien frm = new FormNhanVien();
            //frm.NVDTO = NVDTO;
            frm.ShowDialog();
        }
        private void gọiMónToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormQLGoiMon frm = new FormQLGoiMon();
            frm.NVDTO = NVDTO;
            frm.ShowDialog();
        }
        private void hóaĐơnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormQLDLHoaDon frm = new FormQLDLHoaDon();
            frm.ShowDialog();   
        }
        private void bànToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            FormQLDLBan frm = new FormQLDLBan();
            frm.ShowDialog();
        }
        private void thựcĐơnToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            QuanLyDLThucDon frm = new QuanLyDLThucDon();
            frm.ShowDialog();
        }
        private void nhânViênToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            frmDuLieuXoa frm = new frmDuLieuXoa();
            frm.ShowDialog();
        }
        private void đổiMậtKhẩuToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            frmDoiMK frm = new frmDoiMK();
            frm.NvDTO = NVDTO;
            frm.ShowDialog();
        }

        private void FormQuanLy_Load(object sender, EventArgs e)
        {

        }

        private void thoátToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void liênHệToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormThongTin frm = new FormThongTin();
            frm.ShowDialog();
        }
    }
}
